import Utils from "../utils/Utils";

const {prefixName, assign, defaultTo} = Utils

class URect implements UComponent {

  get name(): string {
    return this.options.name
  }

  get zIndex(): number {
    return this.options.zIndex
  }

  options: Required<UComponent.RectOpt> = {
    x: 0,
    y: 0,
    zIndex: 1,
    name: prefixName('u_rect'),
    width: 50,
    height: 50,
    backgroundColor: 'transparent'
  }

  visible: boolean = true

  constructor(uRectOption?: UComponent.RectOpt) {
    this.setOption(uRectOption)
  }

  
  draw(ctx: CanvasRenderingContext2D) {
    const {x, y, width, height, backgroundColor} = this.options
    
    ctx.save()
    ctx.fillStyle = backgroundColor
    ctx.fillRect(x, y, width, height)
    ctx.restore()
  }
  
  /**
   * 设置图形参数
   * @param uRectOption
   */
  setOption(uRectOption?: UComponent.RectOpt) {
    this.options = assign(this.options, defaultTo(uRectOption, {})) as Required<UComponent.RectOpt>
  }
}

export default URect
